package ccm.component.input;

import ccm.common.Configurable;
import ccm.common.ReceiveDataListener;
import ccm.component.override.JComboBox;
import ccm.component.override.JLabel;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;

abstract public class InputJPanel extends JPanel implements Configurable, ActionListener
{
    protected final ArrayList<ReceiveDataListener> receiveDataListener;
    /**
     * 追加的结尾的下拉菜单
     */
    protected JComboBox<String> endJComboBox;

    InputJPanel()
    {
        super();
        /*
           生成追加的结尾的下拉菜单
         */
        endJComboBox=new JComboBox<>(new String[]{"\r\n","\r","\n",""});
        endJComboBox.setSelectedIndex(0);
        add(endJComboBox);
        endJComboBox.addActionListener(this);
        endJComboBox.setRenderer((list,value,index,isSelected,cellHasFocus)->
        {
            JLabel jLabel=new JLabel(" "+value.replace("\r","\\r").replace("\n","\\n"));
            jLabel.setOpaque(true);
            return jLabel;
        });
        receiveDataListener=new ArrayList<>();
    }

    /**
     * 获取追加的结尾
     *
     * @return 一个字符串表示追加的结尾
     */
    protected String getEndString()
    {
        return (String)endJComboBox.getSelectedItem();
    }

    /**
     * 添加一个接收数据监听器
     *
     * @param listener 监听器
     * @throws NullPointerException 监听器为null时抛出此异常
     */
    public void addReceiveDataListener(ReceiveDataListener listener)
    {
        if(listener==null){return;}
        if(!receiveDataListener.contains(listener)){receiveDataListener.add(listener);}
    }
}
